from selenium import webdriver
from selenium.webdriver.support.relative_locator import locate_with
from selenium.webdriver.common.by import By
from time import sleep
#创建浏览器驱动对象
driver = webdriver.Chrome()
# 窗口最大化
driver.maximize_window()

driver.implicitly_wait(10)

# 相对定位
driver.get("file:///C:/Users/S3214/Desktop/%E7%B4%A0%E6%9D%90/5-4.html")
# #任务1.根据email输入位置定位密码输入框
# loc_elem1 = locate_with(By.TAG_NAME, "input").below({By.ID: "email"})
# driver.find_element(loc_elem1).send_keys("123456")
# 任务2.通过取消按钮，定位登录按钮并点击
# loc_elem2 = locate_with(By.TAG_NAME, "button").below({By.ID: "cancel"})
# loc_elem2 = locate_with(By.TAG_NAME, "button").near({By.ID: "cancel"})
# loc_elem2 = locate_with(By.ID, "submit").to_right_of({By.ID: "cancel"})
# driver.find_element(loc_elem2).click()
# 任务3.bing首页，根据搜索框位置定位搜索按钮，搜索“最年轻亚运会冠军”
# driver.get("http://cn.bing.com")
# driver.find_element(By.ID, "sb_form_q").send_keys("最年轻亚运会冠军")
# loc_bing_browser = locate_with(By.TAG_NAME, "svg").to_right_of({By.ID: "sb_form_q"})
# driver.find_element(loc_bing_browser).click()
# 任务4.根据按钮定位搜索框
# loc_elem4 = locate_with(By.ID, "email").above({By.ID: "cancel"})
# loc_elem4 = locate_with(By.ID, "cancel").below({By.ID: "email"})
# driver.find_element(loc_elem4).send_keys("123456@qq.com")
# driver.find_element(loc_elem4).click()
# 任务5.相对定位组合 根据密码输入框和cancel定位submit
loc_elem5 = locate_with(By.ID, "submit").to_right_of({By.ID: "cancel"}).below({By.ID: "pw"})
driver.find_element(By.ID, "pw").send_keys("123456")
driver.find_element(loc_elem5).click()

sleep(3)
driver.quit()